#!/bin/bash

# capture the existing kernel.pid_max value
sysctl -a 2> /dev/null | grep kernel.pid_max | cut -d " " -f 3 > /home/deck/Android_Waydroid/orig_kernel.pid_max

# set the kernel.pid_max to 65535
sudo sysctl -w kernel.pid_max=65535

# delay before running scripts
while [[ -z $(waydroid shell getprop sys.boot_completed) ]]
do
	sleep 5
done

# This are the commands to execute once Android boot is completed

# Steam Deck controller to work in Waydroid
echo add | tee /sys/devices/virtual/input/input*/event*/uevent

# disable initial device setup via ADB
waydroid shell pm disable com.google.android.setupwizard

# shizuku root
waydroid shell -- sh -c "if [ -f /storage/emulated/0/Android/data/moe.shizuku.privileged.api/start.sh ]
then
	/storage/emulated/0/Android/data/moe.shizuku.privileged.api/start.sh
	fi"

# mantis gamepad pro
waydroid shell -- sh -c "if [ -f /sdcard/Android/data/app.mantispro.gamepad/files/buddyNew.sh ]
then
	/sdcard/Android/data/app.mantispro.gamepad/files/buddyNew.sh
	fi"

# set audio volume to max
waydroid shell -- cmd media_session volume --stream 3 --set 15

# no data permission hack to circumvent the scoped storage permission issue
# Thanks to PrimeOS
waydroid shell -- sh -c "chmod 777 -R /sdcard/Android
chmod 777 -R /sdcard/Android/obb
chmod 777 -R /sdcard/Android/data
chmod 777 -R /data/media/0/Android
chmod 777 -R /data/media/0/Android/obb
chmod 777 -R /data/media/0/Android/data"

# Xbox Wireless Controller spoofing for CoD Mobile to work without 3rd party controller mapping (mantis etc)
if [ -f /home/deck/Android_Waydroid/spoofing/classes.dex ]
then
	cp /home/deck/Android_Waydroid/spoofing/classes.dex /var/lib/waydroid/overlay/system/bin/classes.dex
	waydroid shell -- sh -c "export CLASSPATH=/system/bin/classes.dex ; app_process / com.android.commands.hid.Hid" &

else
	rm /var/lib/waydroid/overlay/system/bin/classes.dex
fi

exit

